package me.stephenj.experiment2.service;

import me.stephenj.experiment2.mapper.*;
import me.stephenj.experiment2.model.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class PersonsService {
    @Autowired
    PersonsMapper personsMapper;

    @Autowired
    PhoneNrsMapper phoneNrsMapper;

    @Autowired
    PhoneNrMapper phoneNrMapper;

    @Autowired
    MailsMapper mailsMapper;

    @Autowired
    MailMapper mailMapper;

    @Autowired
    CompanyMapper companyMapper;

    public List<Person> getPersons() {
        return personsMapper.getAll();
    }

    public Personj getPersonj(int ID) {
        Person person = personsMapper.getOne(ID);
        Personj personj = new Personj(person);

        PhoneNrs phoneNrs = phoneNrsMapper.getAll(person.getPhoneNrsID());
        List<Integer> phoneNrsIDs = phoneNrs.getList();
        for (int phoneNrID: phoneNrsIDs) {
            PhoneNr phoneNr = phoneNrMapper.getPhoneNr(phoneNrID);
            personj.addPhoneNr(phoneNr);
        }

        Mails mails = mailsMapper.getAll(person.getMailsID());
        List<Integer> mailsIDs = mails.getList();
        for (int mailID: mailsIDs) {
            Mail mail = mailMapper.getMail(mailID);
            personj.addMail(mail);
        }

        Company company = companyMapper.getAll(person.getCompanyID());
        personj.addCompany(company);

        return personj;
    }

}
